package com.ctrip.ec.api.mobile.controller;

import com.ctrip.ec.api.mobile.common.RequestContext;
import com.ctrip.ec.api.mobile.model.brand.BrandModel;
import com.ctrip.ec.biz.dao.activity.ActivityDao;
import com.ctrip.ec.biz.entity.activity.ActivityEntity;
import com.ctrip.ec.biz.entity.forward.ForwardActivityEntity;
import com.ctrip.ec.dal.EntityBase;
import com.ctrip.ec.dal.PagerSetting;

import com.ctrip.ec.biz.dao.brand.BrandDao;
import com.ctrip.ec.biz.entity.brand.BrandEntity;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by jiangzy on 2018/9/18.
 */

@RestController
@RequestMapping("brand")
public class BrandController{

    @Autowired
    private BrandDao brand;

    @Autowired
    private ActivityDao activity;

    @Value("${imageDomain}")
    private  String imageDomain;


    /**
     * 需要找到当前正在做活动的品牌，不做活动的不显示
     * @param name
     * @return
     * @throws SQLException
     */
    @GetMapping("list")
    public List<BrandModel> list(@RequestParam(required = false) String name) throws SQLException {

        //找到所有正在做活动的品牌

        String where = null;
        long agentId = RequestContext.getAgentId();

        if(agentId == 0) { //app的品牌列表
             where = "end_time>@0 and agent_id=0";

        }else{ //小程序的品牌列表  -- 转发的活动及自己品牌的活动
             where = "end_time>@0 and ((agent_id=0 and id in (select activity_id from " +
                                    EntityBase.getTableName(ForwardActivityEntity.class) + " where agent_id=" + agentId+ ")) or agent_id=" + agentId + ")";

        }
        List<ActivityEntity>  activityList = activity.list(where, new Timestamp(System.currentTimeMillis()));

        if(activityList  == null || activityList.size()==0){
            return new ArrayList<>();
        }else {
            List<BrandEntity> list = brand.listByIds(activityList.stream().map(t->t.getBrandId()).collect(Collectors.toList()));
            if (!Strings.isNullOrEmpty(name)) {
                list = list.stream().filter(t->t.getName().contains(name)).collect(Collectors.toList());
            }
            List<BrandModel> modelList = new ArrayList<>();
            for (BrandEntity entity : list) {
                entity.setLogoImageUrl(imageDomain + entity.getLogoImageUrl());
                entity.setBannerImageUrl(imageDomain + entity.getBannerImageUrl());
                entity.setCoverImageUrl(imageDomain + entity.getCoverImageUrl());

                BrandModel model = new BrandModel(entity);
                Long activityId = activityList.stream().filter(t->t.getBrandId().equals(entity.getId())).map(t->t.getId()).findFirst().get();
                model.setActivityId(activityId);
                modelList.add(model);
            }
            return modelList;
        }
    }

}
